package experiment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Handler;
import android.widget.TextView;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

import de.tuberlin.julia.SFD_Numbers.BreakView;
import de.tuberlin.julia.SFD_Numbers.EndView;
import de.tuberlin.julia.SFD_Numbers.NumberView;
import de.tuberlin.julia.SFD_Numbers.R;

import experiment.util.ExpParsedConfigDataSet;

public class ApplyDesign {
	
	private static final Logger logger = LoggerFactory.getLogger();	
	
	private int blockCount;
	private int runCount;
	private int pinCount;
	private String enteredDigits;	
	
	private ExpParsedConfigDataSet expDataSet;
	
	NumberView numberView;
	TextView txtView;

	int feedbackDelay;
	String feedbackType;
	String pin;
	public int standardColor;
	
	Intent myIntent;
	
	public ApplyDesign(ExpParsedConfigDataSet expDataSet, NumberView numberView){
		this.numberView = numberView;
		this.txtView = (TextView) numberView.findViewById(R.id.TextViewNumber);
		this.standardColor = txtView.getCurrentTextColor();
		this.expDataSet = expDataSet;
		blockCount = Integer.parseInt(numberView.getIntent().getStringExtra("block"))-1;;
		runCount = Integer.parseInt(numberView.getIntent().getStringExtra("run"))-1;;
		pinCount = 0;
		enteredDigits = "";
		feedbackDelay = expDataSet.getRunArray().get(blockCount).get(runCount);
		feedbackType = expDataSet.getBlockArray().get(blockCount);
		//logger.info("run = " + runCount + " = " + feedbackDelay);
		//logger.info("block = " + blockCount + " = " + feedbackType);
		//logger.info("number = " + blockCount + " = " + feedbackType);
	}
	
	public void enterDigit(String digit){
		enteredDigits = enteredDigits + digit;				
	}
	
	public void compareDigits(Thread t){
		new Thread(new SystemResponse(t)).start();		
	}
	


	public void resetDigits() {
		enteredDigits = "";
		
	}
	
	public int getFeedbackDelay(){
		return feedbackDelay;
	}
	
	public String getFeedbackType(){
		return feedbackType;
	}
		
	public static int max(ArrayList<Integer> t) {
	    int maximum = t.get(0);   // start with the first value
	    for (int i=1; i<t.size(); i++) {
	        if (t.get(i) > maximum) {
	            maximum = t.get(i);   // new maximum
	        }
	    }
	    return maximum;
	}
	
	private class SystemResponse implements Runnable{
		
		private Handler handler = new Handler();
		public Thread feedbackThread;
		
		public SystemResponse(Thread t){
			this.feedbackThread = t;			
		}

		@Override
		public void run() {	
			if(feedbackThread != null){
				try {
					feedbackThread.join();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			pin = expDataSet.getNumberArray().get(blockCount).get(runCount).get(pinCount);
			pinCount = pinCount + 1;				
			
			
			// wait with system response
			try {
				//int waitDur = max(expDataSet.getRunArray().get(blockCount)) + 1;
				//Thread.sleep(waitDur);
				Thread.sleep(5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(enteredDigits.equals(pin)){
				logger.info("correct: " + enteredDigits);
				/*
				handler.post(new Runnable() {					
					@Override
					public void run() {
						logger.info("system response start");
						txtView.setTextColor(numberView.getResources().getColor(R.color.black));		
						txtView.setBackgroundColor(numberView.getResources().getColor(R.color.green));	
						txtView.setText("korrekt!");

					}
				});*/
			}else{
				if ((enteredDigits.equalsIgnoreCase("enter"))||(enteredDigits.equalsIgnoreCase("notenter")))
					logger.info("--------------------------------");	
				else
					logger.info("wrong: " + enteredDigits + " , should be " + pin);
				/*
				handler.post(new Runnable() {					
					@Override
					
					public void run() {
						logger.info("system response start");
						txtView.setTextColor(numberView.getResources().getColor(R.color.black));	
						txtView.setBackgroundColor(numberView.getResources().getColor(R.color.red));	
						txtView.setText("Falsch!");		
						
					}
				});*/
			}
			enteredDigits = "";
			/*
			// wait with reset of display
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
			if(pinCount == expDataSet.getNumberOfPins()){
				pinCount =0;
				runCount++;
				if(((runCount ==  expDataSet.getNumberOfRuns())&&!(expDataSet.getBlockArray().get(blockCount).equals("none"))||(expDataSet.getBlockArray().get(blockCount).equals("none")) && (runCount == 1))){
					pinCount =  0;
					runCount = 0;
					blockCount++;
					if(blockCount == expDataSet.getNumberOfBlocks()){
						myIntent = new Intent(numberView, EndView.class);
						numberView.finish();
						myIntent.putExtra("vpStr", expDataSet.getVPNumber());
						numberView.startActivityForResult(myIntent, 0);
						return;
					}
					
					else{
						feedbackDelay = expDataSet.getRunArray().get(blockCount).get(runCount);
						feedbackType = expDataSet.getBlockArray().get(blockCount);
						myIntent = new Intent(numberView, BreakView.class);
		                myIntent.putExtra("block", blockCount);
		                myIntent.putExtra("run", runCount);
						numberView.startActivityForResult(myIntent, 0);
						//logger.info("end of run");
						//logger.info("end of block");
						//logger.info("run = " + runCount + " = " + feedbackDelay);
						//logger.info("block = " + blockCount + " = " + feedbackType);
						return;
					}
				}
				
				else{
					feedbackDelay = expDataSet.getRunArray().get(blockCount).get(runCount);
					myIntent = new Intent(numberView, BreakView.class);
	                myIntent.putExtra("block", blockCount);
	                myIntent.putExtra("run", runCount);
					numberView.startActivityForResult(myIntent, 0);
					//logger.info("end of run");
					//logger.info("run = " + runCount + " = " + feedbackDelay);
				}
			}else{
				//logger.info("system response end");
				/*
				handler.post(new Runnable() {					
					@Override
					public void run() {
						logger.info("system response end");
						txtView.setTextColor(standardColor);	
						txtView.setBackgroundColor(numberView.getResources().getColor(R.color.black));	
						int pin = pinCount + 1;
						txtView.setText("PIN " + pin);
					}
				});*/
				
			}

		}
	}

}
